#include <wxyDrawing.h>
#include <OTKXmath.h>
#include <wxyutils.h>
#include <wxyAutoIndex.h>

//DimensionStruct成员函数
pfcDimension2D_ptr DimensionStruct::GetDimension()
{
    return dimension;
}
int DimensionStruct::Create(pfcDimension2D_ptr dim)
{
    try
    {
        this->dimension = dim;
        this->location = dim->GetLocation();
        this->symloc = CalSymbolLoc(dim);
        this->view2d = dim->GetView();
        this->viewname = view2d->GetName();
        this->viewcenter = GetViewCenter();
        this->angle = GetAngle();
        this->distance = GetDistance();
        return 1;
    }
    OTK_EXCEPTION_PRINT_LOG
    return 0;
}
pfcPoint3D_ptr DimensionStruct::GetViewCenter()
{
    pfcOutline3D_ptr outline = this->dimension->GetView()->GetOutline();
    viewcenter = pfcPoint3D::create();
    //计算视图中心点
    for (xint k = 0; k < 3; k++)
    {
        pfcPoint3D_ptr p1, p2;
        p1 = outline->get(0);
        p2 = outline->get(1);

        xreal midvalue = (p1->get(k) + p2->get(k)) / 2;
        viewcenter->set(k, midvalue);
    }
    return viewcenter;
}
pfcPoint3D_ptr DimensionStruct::GetSymLoc()
{
    return this->symloc;
}
xstring DimensionStruct::GetViewName()
{
    return this->viewname;
}
double DimensionStruct::GetAngle()
{
    pfcPoint3D_ptr loc = this->symloc;
    double anglearc;
    anglearc = atan2((loc->get(1) - this->viewcenter->get(1)), (loc->get(0) - this->viewcenter->get(0))); //弧度值
    double angledeg;                                                                                      //角度值
    if (anglearc < 0)
        angledeg = anglearc * 180 / PI + 360;
    else
        angledeg = anglearc * 180 / PI;
    angledeg = angledeg - 90;
    if (angledeg < 0)
        angledeg = angledeg + 360;
    angle = round(angledeg / 2) * 2;
    return angledeg;
}
double DimensionStruct::GetDistance()
{
    pfcPoint3D_ptr loc = this->symloc;
    double distance;
    distance = pow((pow((loc->get(0) - this->viewcenter->get(0)), 2) + pow((loc->get(1) - this->viewcenter->get(1)), 2)), 0.5);
    return distance;
}

pfcDetailSymbolInstInstructions_ptr CreateSymbolInstInstruction(pfcModel_ptr mdl)
{
    string path;
    GetDllPath(path);
    path += "symbol";
    xstring SymbolDir = path.c_str();
    pfcModel2D_ptr mdl2D = pfcModel2D::cast(mdl);
    pfcDetailSymbolDefItem_ptr symdef = ((pfcDetailItemOwner_ptr)mdl2D)->RetrieveSymbolDefinition("boolean", SymbolDir, NULL, true);
    pfcDetailSymbolInstInstructions_ptr symins = pfcDetailSymbolInstInstructions::Create(symdef);
    return symins;
}
//修改尺寸位置 纠正SetLocation函数设置尺寸位置时产生偏移
void ChangeDimensionLocation(pfcDimension2D_ptr dimension, pfcPoint3D_ptr dimloc)
{
    dimension->SetLocation(dimloc);
    //纠正更改尺寸时产生的偏差
    pfcPoint3D_ptr dimlocnew = dimension->GetLocation();
    double dx = dimloc->get(0) - dimlocnew->get(0); //计算x偏移值
    double dy = dimloc->get(1) - dimlocnew->get(1); //计算y偏移值
    dimloc->set(0, dimloc->get(0) + dx);            //修正x偏移
    dimloc->set(1, dimloc->get(1) + dy);            //修正y偏移
    dimension->SetLocation(dimloc);
}